package com.test.nnooolwdeaer.bean_wooden.config_info_wooden;

import android.text.TextUtils;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.constant_wooden.Url_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.google.gson.Gson;
import com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.TreeMap;

public class GlobalConfig_wooden {

    private static class Inner_wooden {
        private static GlobalConfig_wooden instance = new GlobalConfig_wooden();
    }

    private GlobalConfig_wooden() {
    }

    public static GlobalConfig_wooden getInstance_wooden() {
        return Inner_wooden.instance;
    }

    public static void parse_wooden(JSONObject jsonObject) {
        if (jsonObject == null) return;
        Inner_wooden.instance.countryConfigTreeMap.clear();
        Gson gson = new Gson();
        Inner_wooden.instance = gson.fromJson(jsonObject.toString(), GlobalConfig_wooden.class);

        if (Inner_wooden.instance.woodenloancommon == null)
            Inner_wooden.instance.woodenloancommon = gson.fromJson(jsonObject.optString(Constant_wooden.WordPrefix_wooden + "common"), Common.class);

        if (Inner_wooden.instance.country == null)
            Inner_wooden.instance.country = new ArrayList<>();
        for (String country : Inner_wooden.instance.country) {
            Inner_wooden.instance.countryConfigTreeMap.put(country, gson.fromJson(jsonObject.optString(country), CountryConfig.class));
        }

    }

    private ArrayList<String> country;
    private Common woodenloancommon;
    private final TreeMap<String, CountryConfig> countryConfigTreeMap = new TreeMap<>();

    public ArrayList<String> getCountryFullPaths_wooden() {
        ArrayList<String> list = new ArrayList<>();
        for (String country : getCountries_wooden()) {
            if (country.endsWith("vnd")) {
                list.add(Url_wooden.Url_Asia + "/" + country);
            } else {
                list.add(Url_wooden.Url_Africa + "/" + country);
            }
        }
        return list;
    }

    public ArrayList<String> getCountries_wooden() {
        if (country == null) return new ArrayList<>();
        return country;
    }

    public TreeMap<String, CountryConfig> getCountryConfigTreeMap_wooden() {
        return countryConfigTreeMap;
    }

    public String[] getPermissionArr_wooden() {
        if (woodenloancommon == null) return new String[0];
        return woodenloancommon.getPermissionArr();
    }

    public String[] getHomeArr_wooden() {
        if (woodenloancommon == null) return new String[0];
        return woodenloancommon.getHomeArr();
    }

    public String isTestAccount_wooden(String mobile) {
        CountryConfig countryConfig = countryConfigTreeMap.get(StaticVariate_wooden.Country_Word_wooden);
        if (countryConfig == null) return "";
        if (mobile.equalsIgnoreCase(countryConfig.getAccM())) {
            return countryConfig.getAccTk();
        }
        return "";
    }

    /**
     * 是否需要申请UsageStats权限
     */
    public Boolean needRequestUsageStatsPermission() {
        if (woodenloancommon == null) return false;
        return woodenloancommon.needRequestUsageStatsPermission();
    }

    /**
     * 获取查询使用统计的天数
     */
    public int getUsageStatsDays() {
        if (woodenloancommon == null) return 7;
        return woodenloancommon.getUsageStatsDays();
    }

    public static class CountryConfig {

        private String accM;
        private String accTk;
        private String customer;

        private CountryConfig() {
        }

        public String getAccM() {
            return StringUtil_wooden.getSafeString_wooden(accM);
        }

        public String getAccTk() {
            return StringUtil_wooden.getSafeString_wooden(accTk);
        }

        public String getCustomer() {
            return StringUtil_wooden.getSafeString_wooden(customer);
        }

        @Override
        public String toString() {
            return "CountryConfig{" +
                    "accM='" + accM + '\'' +
                    ", accTk='" + accTk + '\'' +
                    ", customer='" + customer + '\'' +
                    '}';
        }
    }

    public static class Common {

        private ArrayList<String> permissionGp;
        private ArrayList<String> permission;
        private ArrayList<String> home;
        private String accVersion;
        /**
         * if YES, request PACKAGE_USAGE_STATS permission, or do nothing
         */
        private String usageStatsPermission;
        /**
         * usage stats days
         */
        private int usageStatsDays = 7;

        private Common() {
        }

        public ArrayList<String> getPermissionGp() {
            if (permissionGp == null) return new ArrayList<>();
            return permissionGp;
        }

        public ArrayList<String> getPermission() {
            if (permission == null) return new ArrayList<>();
            return permission;
        }

        public String[] getPermissionArr() {
            if (permission == null) return new String[0];
            String[] result = new String[permission.size()];
            for (int i = 0; i < permission.size(); i++) {
                result[i] = permission.get(i);
            }
            return result;
        }

        public ArrayList<String> getHome() {
            if (home == null) return new ArrayList<>();
            return home;
        }

        public String[] getHomeArr() {
            if (home == null) return new String[0];

            String[] result = new String[home.size()];
            for (int i = 0; i < home.size(); i++) {
                result[i] = home.get(0);
            }
            return result;
        }

        public String getAccVersion() {
            return StringUtil_wooden.getSafeString_wooden(accVersion);
        }

        /**
         * 是否需要申请UsageStats权限
         */
        public boolean needRequestUsageStatsPermission() {
            if (TextUtils.isEmpty(usageStatsPermission)) {
                return false;
            } else {
                return usageStatsPermission.equalsIgnoreCase("YES");
            }
        }

        /**
         * 获取查询使用统计的天数
         */
        public int getUsageStatsDays() {
            return usageStatsDays;
        }
    }
}
